package main

import (
	"context"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	"github.com/spf13/pflag"
	"github.com/spf13/viper"
	"go.uber.org/zap"
	"net/http"
)

func main() {
	initLogger()
	initViper()
	initPrometheous()
	//closeOtel := ioc.InitOtel()

	app := InitWebServer()
	app.Cron.Start()
	for _, c := range app.Consumers {
		err := c.StartBatch()
		if err != nil {
			panic(err)
		}
	}

	//app.Web.Run(":8088")
	server := http.Server{
		Addr:    ":8088",
		Handler: app.Web.Handler(),
	}
	go func() {
		//app.WebAdmin.Start()
	}()
	server.ListenAndServe()

	//ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	//defer cancel()
	//closeOtel(ctx)
	ctx := app.Cron.Stop()
	<-ctx.Done()

	server.Shutdown(context.Background())
}
func initPrometheous() {
	go func() {
		http.Handle("/metrics", promhttp.Handler())
		http.ListenAndServe(":9091", nil)
	}()
}
func initViper() {
	cFile := pflag.String("config", "webook/config/dev.yaml", "指定配置文件路径")
	pflag.Parse()
	viper.SetConfigFile(*cFile)

	if err := viper.ReadInConfig(); err != nil {
		panic(err)
	}
}
func initLogger() {
	logger, _ := zap.NewDevelopment()
	// 如果不replace,啥都打不开xxxxxx
	zap.ReplaceGlobals(logger)
}
